package com.cn.wanxi.service;

import com.cn.wanxi.dao.NewsDao;
import com.cn.wanxi.model.NewsModel;

public class NewsService {
    /**
     * 得到首页的新闻数据
     *
     * @return
     */
    public NewsModel[] getNewsModelsForHomePage() {
        NewsDao newsDao = new NewsDao();
        int length = newsDao.getIdsForHomePage().length;
        NewsModel[] newsModels = new NewsModel[length];
        for (int i = 0; i < length; i++) {
            NewsModel model = new NewsModel();
            model.setId(newsDao.getIdsForHomePage()[i]);
            model.setTitle(newsDao.getTitlesForHomePage()[i]);
            model.setContent(newsDao.getContentsForHomePage()[i]);
            newsModels[i] = model;
        }
        return newsModels;
    }

    /**
     * 根据id查询具体的新闻信息
     *
     * @param id
     * @return
     */
    public NewsModel getNewsModelById(int id) {
        NewsDao newsDao = new NewsDao();
        NewsModel model = new NewsModel();
        for (int i = 0; i < newsDao.getIdsForHomePage().length; i++) {
            if (id == newsDao.getIdsForHomePage()[i]) {
                model.setTitle(newsDao.getTitlesForHomePage()[i]);
                model.setContent(newsDao.getContentsForHomePage()[i]);
            }
        }
        return model;
    }
}
